/*global define*/
define([
	'underscore',
	'backbone',
	'models/book'
], function (_, Backbone, Book) {

	BooksCollection = Backbone.Collection.extend({
		// Reference to this collection's model.
		model: Book,
		
		comparator: function(book1, book2) {
			var sortAttrIndex = this.sortAttrIndex;
			var sortConfig = this.sortConfig;
			var value = 0;
			if (sortAttrIndex != null && typeof sortAttrIndex != undefined && sortConfig) {
				var sortAttr = sortConfig.attrs[sortAttrIndex].attr;
				var asc = sortConfig.attrs[sortAttrIndex].asc;
				var value1 = book1.get(sortAttr);
				var value2 = book2.get(sortAttr);
				if (typeof value1 == "string" && typeof value2 == "string") {
					value = (value1<value2?-1:(value1>value2?1:0)); 
				}
				if (typeof value1 == "number" && typeof value2 == "number") {
					value = value1 - value2;
				}
				if (asc == false) {
					value = -value;
				}
			}
			
			return value;
		},
		
		search : function(letters){
			if(letters == "") return this;
	 
			var pattern = new RegExp(letters,"gi");
			var rs =  this.filter(function(data) {
			  	return pattern.test(data.get("name"));
			});
			return rs;
		}
	});

	return BooksCollection;
});
